package com.store.app.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.R;
import com.store.app.adapter.GuideShoppingHistoryAdapter;
import com.store.app.bean.HistoryBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.ToastUtils;
import com.store.app.widget.AutoListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/2/10.\
 * 历史记录页面
 */
public class HistoryActivity extends BaseActivity implements IOnHttpListener {
    private ImageView public_ll_return;
    private TextView tvTitle;
    private AutoListView lv;
    private List<HistoryBean> list;
    private static final int MSG_LOAD_SUCCESS = 0;
    private static final int MSG_LOAD_FAILED = 1;
    private static final int MSG_REFRESH_SUCCESS = 2;
    private static final int MSG_REFRESH_FAILED = 3;
    private static final int MSG_DELETE_SUCCESS = 6;
    private static final int MSG_DELETE_FAILED = 7;
    private int pageNum;
    private int pageSize;
    private NetRequest netRequest;
    private GuideShoppingHistoryAdapter mAdapter;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_LOAD_SUCCESS:
                    Log.v("zyl", "onLoadComplete");
                    lv.onLoadComplete();
                    if (msg.arg1 == pageNum) {
                        @SuppressWarnings("unchecked")
                        List<HistoryBean> result = (List<HistoryBean>) msg.obj;
                        Log.v("zyl","result:"+result.size());
                        lv.setResultSize(result.size());
                        list.addAll(result);
                        mAdapter.notifyDataSetChanged();
                        pageNum += 1;
                        if (list.size() == 0) {
//							lin_loadfail.setVisibility(View.VISIBLE);
//							img_fail_nonet.setVisibility(View.GONE);
//							img_fail_notext.setVisibility(View.VISIBLE);
//							tv_fail.setText("暂无内容");
                        }
                    }
                    break;
                case MSG_LOAD_FAILED:
                    lv.setResultSize(0);
                    lv.onLoadComplete();
                    Toast.makeText(HistoryActivity.this, (String) msg.obj, Toast.LENGTH_SHORT).show();
                    break;
                case MSG_REFRESH_SUCCESS:
                    lv.onLoadComplete();
                    lv.onRefreshComplete();
                    @SuppressWarnings("unchecked")
                    List<HistoryBean> result = (List<HistoryBean>) msg.obj;
                    lv.setResultSize(result.size());
                    list.clear();
                    list.addAll(result);
                    mAdapter.notifyDataSetChanged();
                    pageNum += 1;
                    break;
                case MSG_REFRESH_FAILED:
                    lv.onRefreshComplete();
                    Toast.makeText(HistoryActivity.this, (String) msg.obj, Toast.LENGTH_SHORT).show();
                    break;
                case MSG_DELETE_SUCCESS:
                    lv.onRefresh();
                    break;
                case MSG_DELETE_FAILED:
                    Toast.makeText(HistoryActivity.this, (String) msg.obj, Toast.LENGTH_SHORT).show();
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        initData();
        initView();
        setListeners();
        lv.onLoad();
    }

    private void initData() {
        list = new ArrayList<>();
        pageNum = 1;
        pageSize = 15;
        netRequest = new NetRequest(this);
    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        lv = (AutoListView) findViewById(R.id.lv_message);
        tvTitle.setText("历史记录");
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mAdapter = new GuideShoppingHistoryAdapter(list, this);
        lv.setAdapter(mAdapter);
        Log.v("zyl", "initView onLoad");

    }

    private void setListeners() {


        lv.setOnRefreshListener(new AutoListView.OnRefreshListener() {

            @Override
            public void onRefresh() {
                pageNum = 1;
                netRequest.requestSaleAssistantHistoryPageFind(2, pageNum + "", pageSize + "");
            }
        });

        lv.setOnLoadListener(new AutoListView.OnLoadListener() {

            @Override
            public void onLoad() {
                Log.v("zyl", " onLoad");
                netRequest.requestSaleAssistantHistoryPageFind(1, pageNum + "", pageSize + "");
            }
        });
    }

    @Override
    public void onAfinalFail() {
        ToastUtils.showTextToast(this, "请检查本地网络");
        if (lv != null) {
            lv.onRefreshComplete();
        }
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if (requestId == 1) {
            try {
                JSONObject data = new JSONObject(result);
                Log.v("zyl", "data1:" + data.toString());
                Gson gson = new Gson();
                JSONArray listArr = data.getJSONArray("list");
                List<HistoryBean> OrderBeanList = new ArrayList<>();
                for (int i = 0; i < listArr.length(); i++) {
                    HistoryBean orderBean = gson.fromJson(listArr.getJSONObject(i).toString(), HistoryBean.class);
                    OrderBeanList.add(orderBean);
                }

                Message message = Message.obtain();
                message.what = MSG_LOAD_SUCCESS;
                message.arg1 = data.getJSONObject("page").getInt("page_no");
                message.obj = OrderBeanList;
                mHandler.sendMessage(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestId == 2) {
            try {
                JSONObject data = new JSONObject(result);
                Gson gson = new Gson();
                JSONArray listArr = data.getJSONArray("list");
                List<HistoryBean> OrderBeanList = new ArrayList<>();
                for (int i = 0; i < listArr.length(); i++) {
                    HistoryBean orderBean = gson.fromJson(listArr.getJSONObject(i).toString(), HistoryBean.class);
                    OrderBeanList.add(orderBean);
                }
                Message message = Message.obtain();
                message.what = MSG_REFRESH_SUCCESS;
                message.arg1 = data.getJSONObject("page").getInt("page_no");
                message.obj = OrderBeanList;
                mHandler.sendMessage(message);
            } catch (Exception e) {
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {

    }
}
